﻿using UnityEngine;
using System.Collections;

public class Camera : MonoBehaviour {


	public Transform cuboPrefab;
	public GameObject player;
	public Animator anim;
	Touch touch;
	Vector2 touchStartPos;
	Vector2 direction;

	bool canJump = false;
	bool canCreateCube = false;

	public GUIStyle customButton;

	void Start ()
	{
		anim = player.GetComponent<Animator>();
	}

	void Update () 
	{
		if(Input.touchCount > 0) 
		{
			touch = Input.GetTouch(0);
		}

		switch(touch.phase)
		{
			case TouchPhase.Began:

				touchStartPos = touch.position;
							
				player.GetComponent<Player>().animationTime += 0.5f;

				canCreateCube = true;

			break;

			case TouchPhase.Moved:
				
				direction = touch.position - touchStartPos;

				canJump = true;

			break;

			case TouchPhase.Ended:

				direction = Vector2.zero;
				canJump = false;
				canCreateCube = false;
		
			break;
		 }

		if (direction.y > 0 && canJump ) 
		{
			player.GetComponent<Player>().jump = true;
		}

		if(touchStartPos == touch.position && player.GetComponent<Player>().animationTime == 0)
		{
			anim.SetTrigger("Atirando");
			player.GetComponent<Player>().cubeTime += 0.5f;
		}
		//if(Input.GetMouseButtonDown(0) && player.GetComponent<Player>().cubecreate && canCreateCube)
		if(touchStartPos == touch.position && player.GetComponent<Player>().cubeTime == 0 && canCreateCube)
		{
			Vector3 origin = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
			origin.z = 0;
			Instantiate(cuboPrefab, origin, Quaternion.identity);
		}
	}
	void OnGUI()
	{
		GUI.Label(new Rect(Screen.width*0.001f, Screen.height*0.01f, 200, 50), "CuboTime: " + player.GetComponent<Player>().cubeTime, customButton);
		GUI.Label(new Rect(Screen.width*0.001f, Screen.height*0.1f, 200, 50), "AnimationTime: " + player.GetComponent<Player>().animationTime, customButton);
	}

}